﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace  RzCorportativo.Recursos.h
{
    /// <summary>
    /// Summary description for Upload
    /// </summary>
    public class Upload : IHttpHandler
    {
        private const string APP_PATHKEY = "uploadPath";
        private const string APP_IMGSRCKEY = "uploadImgSrc";

        private static string Path
        {
            get
            {
                return System.Web.Configuration.WebConfigurationManager.AppSettings[Upload.APP_PATHKEY];
            }
        }

        private static string ImageSrc
        {
            get
            {
                return System.Web.Configuration.WebConfigurationManager.AppSettings[Upload.APP_IMGSRCKEY];
            }
        }

        public void ProcessRequest(HttpContext context)
        {
            HttpPostedFile uploads = context.Request.Files["upload"];
            string ckEditorFuncNum = context.Request["CKEditorFuncNum"];
            string fileName = System.IO.Path.GetFileName(uploads.FileName);
            uploads.SaveAs(Upload.Path + fileName);
            string url = EFramework.Web.UrlBase.UrlPadrao + Upload.ImageSrc + fileName;
            context.Response.Write("<script>window.parent.CKEDITOR.tools.callFunction(" + ckEditorFuncNum + ", \"" + url + "\");</script>");
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}